package shuiyuqing.network.http;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xkh.xkh.MyApplication;

import managers.UIHandler;
import managers.VolleySingleton;
import shuiyuqing.network.beans.HistoryIrrBRWaterDam;

/**
 * 获取历史信息
 * Created by 凯 on 2015/11/11.
 */
public class GetHistoryWaterDam {


    Delegate delegate;

    // 上行数据
    Upload upload;

    // 下行数据
    Download download = new Download();

    public void setDownload(Download download) {
        this.download = download;
    }

    public Download getDownload() {
        return download;
    }

    public GetHistoryWaterDam(Delegate delegate, Upload upload) {
        this.delegate = delegate;
        this.upload = upload;
    }

    // 获取历史水情信息
    public void get() {
        if (delegate != null)
        {
            ((UIHandler.BaseActivity)delegate).showDialog();
        }

        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/GetHistoryWaterGateDam";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                        {
                            ((UIHandler.BaseActivity)delegate).closeDialog();
                        }
                        afterGet(response);
                        if (delegate != null) {
                            delegate.onResponse(response, GetHistoryWaterDam.this);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null) {
                    delegate.onErrorResponse(error, GetHistoryWaterDam.this);
                }
                if (delegate != null)
                {
                    ((UIHandler.BaseActivity)delegate).closeDialog();
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
//                map.put("data", stadiacode);
                map.put("data", JSON.toJSONString(upload));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 下载后
    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
        if (download.getStatus() == DownloadInfo.SUCCESS) {

        } else if (download.getStatus() == DownloadInfo.FAILED) {

        }
    }


    public static class Upload {
        // 测站编码
        String stadiaCode;

        // 查询时间，某一天
        String date;

        // 是否是第一次
        int first;//0 第一次;其他不是

        // 开始的时间戳
        long stamp;

        public String getStadiaCode() {
            return stadiaCode;
        }

        public void setStadiaCode(String stadiaCode) {
            this.stadiaCode = stadiaCode;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public int getFirst() {
            return first;
        }

        public void setFirst(int first) {
            this.first = first;
        }

        public long getStamp() {
            return stamp;
        }

        public void setStamp(long stamp) {
            this.stamp = stamp;
        }

    }


    // 下行数据
    public static class Download {

        int status;
        List<HistoryIrrBRWaterDam> dams = new ArrayList<HistoryIrrBRWaterDam>();

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public List<HistoryIrrBRWaterDam> getDams() {
            return dams;
        }

        public void setDams(List<HistoryIrrBRWaterDam> dams) {
            this.dams = dams;
        }

    }

    // 提高回调接口
    public static interface Delegate {
        public void onResponse(String response, GetHistoryWaterDam dam);

        public void onErrorResponse(VolleyError error, GetHistoryWaterDam dam);
    }
}
